package com.panda.pandawash.fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.panda.pandawash.R;
import com.panda.pandawash.adapter.ListAdapter;
import com.xw.repo.refresh.PullListView;
import com.xw.repo.refresh.PullToRefreshLayout;

import java.util.ArrayList;
import java.util.List;

public class MessageFragment extends Fragment implements PullToRefreshLayout.OnRefreshListener,AdapterView.OnItemClickListener{
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    //
    private PullToRefreshLayout mRefreshLayout;
    private PullListView mPullListView;

    private List<String> mStrings;
    private ListAdapter mAdapter;
    //

    // TODO: Rename and change types of parameters
    private String mParam1;

    private OnFragmentInteractionListener mListener;

    public MessageFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment MessageFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MessageFragment newInstance() {
        MessageFragment fragment = new MessageFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }

    }

    View view;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        //view = inflater.inflate(R.layout.fragment_message, container, false);
        view = inflater.inflate(R.layout.message_fragment_layout, container, false);
        mRefreshLayout = (PullToRefreshLayout) view.findViewById(R.id.pullToRefreshLayout);
        mPullListView = (PullListView) view.findViewById(R.id.pullListView);
        mStrings = new ArrayList<>();
        mPullListView.setOnItemClickListener(this);
        mRefreshLayout.setOnRefreshListener(this);

        mAdapter = new ListAdapter(getActivity(), mStrings);
        mPullListView.setAdapter(mAdapter);
        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
 //       Toast.makeText(getActivity(), "" + i + "", Toast.LENGTH_SHORT).show();
//        if(newsBeanList.size()<i){
//            return;
//        }
//        Intent intent=new Intent(getActivity(), WebViewActivity.class);
//        intent.putExtra("url",newsBeanList.get(i).getNewurl());
//        intent.putExtra("title", newsBeanList.get(i).getTitle());
//        intent.putExtra("content", newsBeanList.get(i).getContent());
//        startActivity(intent);
    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        mRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                //mStrings.add("refresh item " + mStrings.size());
                //mStrings.add("refresh item " + mStrings.size());
                mStrings.add(getActivity().getString(R.string.message_fragment_title) + (mStrings.size()+1));
                mStrings.add(getActivity().getString(R.string.message_fragment_title) + (mStrings.size()+1));

                mRefreshLayout.refreshFinish(true);
                updateListData();
            }
        }, 2000);
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        mRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
//                mStrings.add("load item " + mStrings.size());
//                mStrings.add("load item " + mStrings.size());
                mStrings.add(getActivity().getString(R.string.message_fragment_title) + (mStrings.size()+1));
                mStrings.add(getActivity().getString(R.string.message_fragment_title) + (mStrings.size()+1));

                mRefreshLayout.loadMoreFinish(true);
                updateListData();

            }
        }, 2000);
    }

    private void updateListData() {
        if (mAdapter == null) {
            mAdapter = new ListAdapter(getActivity(), mStrings);
            mPullListView.setAdapter(mAdapter);
        } else {
            mAdapter.updateListView(mStrings);
        }
    }

    // ============================================

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
